#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UIPushButton.h>
#import <UIKit/UIThreePartButton.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UINavBarButton.h>
#import <UIKit/UINavigationItem.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIButtonBar.h>
#import <UIKit/UITextView.h>
#import <UIKit/UITransitionView.h>
#import <UIKit/UIImageView.h>
#import <UIKit/UIKeyboard.h>

#include <unistd.h>
#import "CardView.h"

@implementation CardView : UIView

- (id) initWithDrillView:(DrillView*)drillView withFrame:(struct CGRect)rect withHeight:(float)height
{
	struct CGRect cardRect = CGRectMake(0.0f, 0.0f, 320.0f, height);
	[super initWithFrame: cardRect];
	
	// Store the question and answer
	_drillView = drillView;
	
	_answer = @"";
	_question = @"";
	_answer_displayed = NO;
	
	_answerMessage = @"Tap twice to see the answer.";
	
	// Set up Question view
	_questionView = [[UITextLabel alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, height / 2.0f - 16.0f)];
	[_questionView setTapDelegate:self];
	[_questionView setWrapsText:YES];
	
	// Set up answer transition view
	_answerTransition = [[UITransitionView alloc] initWithFrame: CGRectMake(0.0f, (height / 2.0f) - 15.0f, 320.0f, height / 2.0f + 15.0f)];
	[_answerTransition setTapDelegate:self];
	
	_answerView = [[UITextLabel alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, height / 2.0f + 15.0f)];
	[_answerView setTapDelegate:self];
	[_answerView setWrapsText:YES];
	
	
	_hiddenAnswerView = [[UITextLabel alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, height / 2.0f + 15.0f)];
	[_hiddenAnswerView setTapDelegate:self];
	[_hiddenAnswerView setText:_answerMessage];
	
	
	[self addSubview:_questionView];
	[self addSubview:_answerTransition];
	
	[self setTapDelegate:self];
	
	return self;

}

/**
 * Displays the answer to the question
**/
- (void) toggleAnswer
{
	if (!_answer_displayed){
		[_answerTransition transition:3 fromView:_hiddenAnswerView toView:_answerView];
		_answer_displayed = YES;
	} else {
		[_answerTransition transition:7 fromView:_answerView toView:_hiddenAnswerView];
		_answer_displayed = NO;
	}
}

/**
 * Set Queston
**/
- (void) setQuestion:(NSString*)question andAnswer:(NSString*)answer
{
	_question = question;
	_answer = answer;
	[self resetCard];
}

/**
 * Resets the card
**/
- (void) resetCard
{
	[_questionView setText:_question];
	[_answerView setText:_answer];
	[_answerTransition transition:3 toView:_hiddenAnswerView];
	_answer_displayed = NO;
}


/**
 * Tap/Swipe delegate methods
**/
- (void)view:(UIView *)view handleTapWithCount:(int)count event:(struct __GSEvent *)event 
{
	NSLog(@"Tap with count %i is now being handeled.",count);	
	if (count == 2) {
		[self toggleAnswer];
	}
}

- (BOOL) canHandleSwipes 
{
	return YES;
}

- (int)swipe:(int)direction withEvent:(struct __GSEvent*)event
{
	NSLog(@"A swipe with direction %i occurred.",direction);
	switch(direction)
	{
		case 4: // A 'next' card swipe
			[_drillView nextQuestion];
			break;
		case 8:	// A 'back' card swipe
			[_drillView backQuestion];
			break;
	}
}

@end